class SimpleMatrix {
	matrix = null;
	returnFullInfo = true;
	constructor(arr) {
		// 创建新数组而不是修改输入参数
		this.matrix = arr.map((row, i) => 
			row.map((val, j) => ({
				x: i,
				y: j,
				val: val
			})
		));
	}

	// 获取数组
	getMatrix() {
		return this.matrix;
	}

	// 获取列
	getCol(col, f = this.returnFullInfo) {
		this.checkCol(col);
		let newArr = [];
		for (let i = 0; i < this.matrix.length; i++) {
			for (let j = 0; j < this.matrix[i].length; j++) {
				if (j == col) {
					if (f) {
						newArr = [...newArr, this.matrix[i][j]]
					} else {
						newArr = [...newArr, this.matrix[i][j].val]
					}
				}
			}
		}
		return newArr;
	}

	// 获取行
	getRow(row, f = this.returnFullInfo) {
		this.checkRow(row);
		let newArr = [];
		for (let i = 0; i < this.matrix.length; i++) {
			for (let j = 0; j < this.matrix[i].length; j++) {
				if (i == row) {
					if (f) {
						newArr = [...newArr, this.matrix[i][j]]
					} else {
						newArr = [...newArr, this.matrix[i][j].val]
					}
				}
			}
		}
		return newArr;
	}

	// 获取四周
	getRound([row, col], f = this.returnFullInfo) {
		this.checkAll([row, col]);
		const result = [];
		for (let i = row - 1; i <= row + 1; i++) {
			for (let j = col - 1; j <= col + 1; j++) {
				const isValid = i >= 0 && i < this.matrix.length && 
							   j >= 0 && j < this.matrix[0].length &&
							   !(i === row && j === col); // 排除自身位置
				if(isValid){
					const item = this.matrix[i][j];
					result.push(f ? item : item.val);
				}
			}
		}
		return result;
	}

	// 获取相邻的有效面积，传入高阶函数
	getAdjoinArea(){
		
	}
	
	
	
	// 验证
	checkCol(v) {
		if (v >= this.matrix[0].length || v < 0) {
			throw new Error("SimpleMatrixJS: Column index out of bounds");
		}
	}
	
	checkRow(v) {
		if (v >= this.matrix.length || v < 0) {
			throw new Error("SimpleMatrixJS: Row index out of bounds");
		}
	}
	
	checkAll([row, col]) {
		this.checkRow(row);
		this.checkCol(col);
	}

	print(matrix) {
		let output = '\n';
		// 计算每列最大宽度
		const colWidths = [];
		for(let j = 0; j < matrix[0].length; j++) {
			let maxWidth = 0;
			for(let i = 0; i < matrix.length; i++) {
				maxWidth = Math.max(maxWidth, String(matrix[i][j].val).length);
			}
			colWidths[j] = maxWidth;
		}
		
		// 添加上边框
		output += '┌';
		for(let j = 0; j < matrix[0].length; j++) {
			output += '─'.repeat(colWidths[j] + 2);
			if(j < matrix[0].length - 1) output += '┬';
		}
		output += '┐\n';
		
		// 添加内容
		for(let i = 0; i < matrix.length; i++) {
			output += '│';
			for(let j = 0; j < matrix[i].length; j++) {
				const val = String(matrix[i][j].val);
				const padding = ' '.repeat(colWidths[j] - val.length);
				output += ' ' + val + padding + ' ';
				if(j < matrix[i].length - 1) output += '│';
			}
			output += '│\n';
			
			// 添加行分隔符
			if(i < matrix.length - 1) {
				output += '├';
				for(let j = 0; j < matrix[0].length; j++) {
					output += '─'.repeat(colWidths[j] + 2);
					if(j < matrix[0].length - 1) output += '┼';
				}
				output += '┤\n';
			}
		}
		
		// 添加下边框
		output += '└';
		for(let j = 0; j < matrix[0].length; j++) {
			output += '─'.repeat(colWidths[j] + 2);
			if(j < matrix[0].length - 1) output += '┴';
		}
		output += '┘\n';
		
		console.log(output);
	}
		
	/**
	 * 获取指定位置相对的元素
	 * @param {number} row - 起始行索引
	 * @param {number} col - 起始列索引
	 * @param {string} direction - 方向：'up' | 'down' | 'left' | 'right' | 'up-left' | 'up-right' | 'down-left' | 'down-right'
	 * @param {number} steps - 步数（正整数）
	 * @param {boolean} [f=this.returnFullInfo] - 是否返回完整信息
	 * @returns {object|number|undefined} 目标位置的元素
	 */
	getRelativeElement([row, col], direction, steps = 1, f = this.returnFullInfo) {
		this.checkAll([row, col]);
		
		if (steps < 1) {
			throw new Error("Steps must be a positive integer");
		}

		let targetRow = row;
		let targetCol = col;

		switch(direction.toLowerCase()) {
			case 'up':
				targetRow -= steps;
				break;
			case 'down':
				targetRow += steps;
				break;
			case 'left':
				targetCol -= steps;
				break;
			case 'right':
				targetCol += steps;
				break;
			case 'up-left':
				targetRow -= steps;
				targetCol -= steps;
				break;
			case 'up-right':
				targetRow -= steps;
				targetCol += steps;
				break;
			case 'down-left':
				targetRow += steps;
				targetCol -= steps;
				break;
			case 'down-right':
				targetRow += steps;
				targetCol += steps;
				break;
			default:
				throw new Error("Invalid direction. Valid directions are: 'up', 'down', 'left', 'right', 'up-left', 'up-right', 'down-left', 'down-right'");
		}

		// 检查目标位置是否有效
		const isValid = targetRow >= 0 && 
					   targetRow < this.matrix.length && 
					   targetCol >= 0 && 
					   targetCol < this.matrix[0].length;

		if (!isValid) {
			return undefined;
		}

		return f ? this.matrix[targetRow][targetCol] : this.matrix[targetRow][targetCol].val;
	}
		
};

if (typeof module !== 'undefined' && module.exports) {
	module.exports = SimpleMatrix;
}

